Array dump_json Method
Syntax
C dump_json(C format [,C filter])
Arguments
- formatCharacter
A pattern that defines how to format the data in the array as a JSON object. The pattern is defined as a JSON string.
- filterCharacter
Default = all elements. A logical expression that uses any of the array properties.
Returns
- resultCharacter
Returns the array data formatted as a JSON object using the specified pattern.
Description
Creates a JSON string from data in a property array.
Discussion
The .dump_json() method creates a JSON string from data in a property array (this method is similar to the .dump_properties() method). You must specify a pattern to define the format of the JSON to generate.
For example, assume the following JSON object represents the data in an Xbasic property array.
dim json as c = <<%str% [ {"Firstname": "John", "Lastname" : "Smith", "Address": { "Street" : "14 Main Street", "City" : "Boston", "State" : "MA"}}, {"Firstname": "Henry", "Lastname" : "Rhodes", "Address": { "Street" : "4 Circle Drive", "City" : "New York", "State" : "NY"}}, {"Firstname": "Allison", "Lastname" : "Berman", "Address": { "Street" : "11 Beaver Avenue", "City" : "Los Angeles", "State" : "CA"}} ] %str%
This JSON can be parsed into an Xbasic property array as follows:
dim p as p = json_parse(json)
The pattern shown below can be used to define the required format of the JSON to be dumped out using the .dump_json() method:
dim pattern as c = <<%str% { "Lastname": "", "Married": true, "Address": { "City": "" } } %str%
Notice that the pattern can specify property names (and corresponding default value) for properties that may not exist in the source property array. For example, the pattern specifies a "Married" property with a default value of true.
The .dump_json() method can be called on the Xbasic property array. For example:
var resultJSON = p.dump_json(pattern)
The resulting JSON from the method is shown below:
[ { "Lastname": "Smith", "Married": true, "Address": { "City": "Boston" } }, { "Lastname": "Rhodes", "Married": true, "Address": { "City": "New York" } }, { "Lastname": "Berman", "Married": true, "Address": { "City": "Los Angeles" }, } ]